package libs;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *              This class is designed to check existence of registration reference
 *              in database.
 *
 * @author      Jan Papousek
 * @version     2009-03-31
 */
public class RegistrationExistence extends AbstractExistence {

    public RegistrationExistence(Connection conn, Student student, Seminar seminar) throws SQLException {
        if (conn == null) {
            throw new NullPointerException("Argument 'conn' is null.");
        }
        if (student == null) {
            throw new NullPointerException("Argument 'student' is null.");
        }
        if (seminar == null) {
            throw new NullPointerException("Argument 'seminar' is null.");
        }

        PreparedStatement preparedSt = null;
        ResultSet resultSet = null;

        // check student's registration
        try {
            preparedSt = conn.prepareStatement("select count(*) from seminar_registrations" +
                    " where subject_code=? and student=? and seminar_number=?");
            preparedSt.setString(1, seminar.getSubject().getCode());
            preparedSt.setInt(2, student.getId());
            preparedSt.setInt(3, seminar.getNumber());
            resultSet = preparedSt.executeQuery();
            resultSet.next();
            if(resultSet.getInt(1) != 0) {
                setExistence(true);
            }
        } finally {
            if(preparedSt != null) {
                preparedSt.close();
            }
        }
    }

}
